package com.itheima.prize.api.action;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.prize.commons.db.entity.CardUser;
import com.itheima.prize.commons.db.entity.CardUserDto;
import com.itheima.prize.commons.db.entity.ViewCardUserHit;
import com.itheima.prize.commons.db.mapper.ViewCardUserHitMapper;
import com.itheima.prize.commons.db.service.GameLoadService;
import com.itheima.prize.commons.db.service.ViewCardUserHitService;
import com.itheima.prize.commons.utils.ApiResult;
import com.itheima.prize.commons.utils.PageBean;
import com.itheima.prize.commons.utils.RedisUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@RestController
@RequestMapping(value = "/api/user")
@Api(tags = {"用户模块"})
public class UserController {

    @Autowired
    private ViewCardUserHitService hitService;
    @Autowired
    private GameLoadService loadService;

    @GetMapping("/info")
    @ApiOperation(value = "用户信息")
    public ApiResult info(HttpServletRequest request) {
        //TODO：任务3.3-用户模块-用户信息 (完成)
        HttpSession session = request.getSession();//获取session
        CardUser user = (CardUser) session.getAttribute("user");//获取session中的用户
        if (user == null){
                return new ApiResult(0, "登录超时",null);
        }else {
            CardUserDto dto = new CardUserDto(user);
            dto.setGames(loadService.getGamesNumByUserId(user.getId()));//获取用户参与活动数量
            dto.setProducts(loadService.getPrizesNumByUserId(user.getId()));//获取用户中奖数量
            return new ApiResult(1, "成功",dto);
        }
    }

    @GetMapping("/hit/{gameid}/{curpage}/{limit}")
    @ApiOperation(value = "我的奖品")
    @ApiImplicitParams({
            @ApiImplicitParam(name="gameid",value = "活动id（-1=全部）",dataType = "int",example = "1",required = true),
            @ApiImplicitParam(name = "curpage",value = "第几页",defaultValue = "1",dataType = "int"),
            @ApiImplicitParam(name = "limit",value = "每页条数",defaultValue = "10",dataType = "int")
    })
    public ApiResult hit(@PathVariable int gameid,@PathVariable int curpage,@PathVariable int limit,HttpServletRequest request) {//@PathVariable表示这三个参数是从 URL 路径中获取的
        //TODO：任务3.4-用户模块-我的奖品 （完成）
        HttpSession session = request.getSession();
        CardUser user = (CardUser) session.getAttribute("user");
        QueryWrapper<ViewCardUserHit> wrapper = new QueryWrapper<>();
        wrapper.eq("userid",user.getId());
        if (gameid != -1){
            wrapper.eq("gameid",gameid);
        }
        Page<ViewCardUserHit> all = hitService.page(new Page(curpage,limit),wrapper);//new Page(curpage, limit)：创建一个分页参数对象，告诉系统 "我要查第 curpage 页，每页显示 limit 条数据"（比如 curpage=2、limit=10，就是查第 2 页，每页 10 条）。
        //把这个分页参数和之前的查询条件（wrapper）一起传给hitService.page()方法，这个方法会去数据库里查询符合条件的记录，并自动做好分页处理。
        return new ApiResult(1, "成功",new PageBean<ViewCardUserHit>(all));//new PageBean<ViewCardUserHit>(all)：把分页查询的结果（all）转换成前端更容易处理的分页格式（PageBean），里面会清晰包含 "当前页数据、总条数、总页数、当前页码" 等信息。

    }

}